﻿using ASP;
using ClassLibrary.BusinessLayer.Facade;
using IPMS.AccessAuth;
using IPMS.BLL;
using Resources;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Globalization;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web;
using SysOperationLayer.DBHandle;
using SysComm;


public partial class CountriesAdd : Page, IRequiresSessionState
{
    LogDB m_logdb = new LogDB();
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setButtons(string.Empty);
        this.setTextReadOnly(false);
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "Countries");
        if (!canDelete.CanDelete())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoDelete.ToString();
        }
        else
        {
            try
            {
                this.ODSCountries.DeleteMethod = "Delete";
                this.ODSCountries.DeleteParameters["IsDelete"].DefaultValue = "true";
                this.ODSCountries.DeleteParameters["DeleteBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSCountries.DeleteParameters["DeleteIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
                this.ODSCountries.DeleteParameters["Original_Country"].DefaultValue = this.txtCountry.Text.ToString();
                this.ODSCountries.Delete();
                this.clearText();
                this.Search();
                this.setButtons(string.Empty);
                this.setTextReadOnly(true);
            }
            catch (Exception ex)
            {
                List<string> lstValues = new List<string>();
                //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
                this.lblError.Text = ex.Message.ToString();
                this.chkForOperation.Checked = true;
            }
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "Countries");
        if (!canDelete.CanUpdate())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoEdit.ToString();
        }
        else
        {
            this.setButtons("modify");
            this.setTextReadOnly(false);
            this.txtCountry.ReadOnly = true;
            this.txtCountryEng.Focus();
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Auth canDelete = new Auth(this.Session["UserId"].ToString(), string.Empty, "Countries");
        if (!canDelete.CanAdd())
        {
            this.lblError.Visible = true;
            this.lblError.Text = Messages.mNoAdd.ToString();
        }
        else
        {
            this.setButtons("new");
            this.setTextReadOnly(false);
            this.clearText();
            this.txtCountry.Focus();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string trueUpdateStr = string.Empty;
            string falseUpdateStr = string.Empty;
            string trueInsertStr = string.Empty;
            string falseInsertStr = string.Empty;
            if (Session["Culture"] != null)
            {

                if (Session["Culture"].ToString() == "zh-TW")
                {
                    trueUpdateStr = "更新成功!";
                    falseUpdateStr = "更新失败!";
                    trueInsertStr = "添加成功!";
                    falseInsertStr = "添加失敗!";
                }
                else if (Session["Culture"].ToString() == "zh-CN")
                {
                    trueUpdateStr = "更新成功!";
                    falseUpdateStr = "更新失败!";
                    trueInsertStr = "添加成功!";
                    falseInsertStr = "添加失败!";
                }
                else
                {
                    trueUpdateStr = "Update succeed!";
                    falseUpdateStr = "Update failed!";
                    trueInsertStr = "Add succeed!";
                    falseUpdateStr = "Add failed!";
                }
            }

            if (this.chkForUpdate.Checked)
            {
                this.ODSCountries.UpdateMethod = "Update";
                this.ODSCountries.UpdateParameters["EnglishName"].DefaultValue = this.txtCountryEng.Text;
                this.ODSCountries.UpdateParameters["ChineseName"].DefaultValue = this.txtCountryChi.Text;
                this.ODSCountries.UpdateParameters["PCTYN"].DefaultValue = this.chkPCTYN.Checked ? "true" : "false";
                this.ODSCountries.UpdateParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSCountries.UpdateParameters["Original_Country"].DefaultValue = this.txtCountry.Text;
                this.ODSCountries.UpdateParameters["UpdateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
                if (this.ODSCountries.Update() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueUpdateStr, "Countries.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_country, "update countrie succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_country, "update countrie faile!");
                }
            }
            else
            {
                this.ODSCountries.InsertMethod = "Insert";
                this.ODSCountries.InsertParameters["Country"].DefaultValue = this.txtCountry.Text;
                this.ODSCountries.InsertParameters["EnglishName"].DefaultValue = this.txtCountryEng.Text;
                this.ODSCountries.InsertParameters["ChineseName"].DefaultValue = this.txtCountryChi.Text;
                this.ODSCountries.InsertParameters["PCTYN"].DefaultValue = this.chkPCTYN.Checked ? "true" : "false";
                this.ODSCountries.InsertParameters["CreateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSCountries.InsertParameters["UpdateBy"].DefaultValue = this.Page.User.Identity.Name.ToString();
                this.ODSCountries.InsertParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
                this.ODSCountries.InsertParameters["CreateIP"].DefaultValue = HttpContext.Current.Request.UserHostAddress;
                if (this.ODSCountries.Insert() < 0)
                {
                    IPMS.Common.Jscript.AlertAndRedirect(trueInsertStr, "Countries.aspx", this.Page);
                    m_logdb.writeSucLog(SysUtil.m_functiong_country, "add countrie succeed!");
                }
                else
                {
                    IPMS.Common.Jscript.Alert(falseInsertStr, this.Page);
                    m_logdb.writeFaileLog(SysUtil.m_functiong_country, "add countrie faile!");
                }     
            }
        }
        catch (Exception ex)
        {
            List<string> lstValues = new List<string>();
            //this.lblError.Text = IPDataFormat.GenerateMessage(ex, lstValues);
            this.lblError.Text = ex.Message.ToString();
            this.chkForOperation.Checked = true;
        }
        this.setTextReadOnly(false);
        this.setButtons(string.Empty);
        this.chkForUpdate.Checked = true;
        this.clearText();
        this.Search();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.Search();
    }

    protected void clearText()
    {
        this.txtCountry.Text = string.Empty;
        this.txtCountryEng.Text = string.Empty;
        this.txtCountryChi.Text = string.Empty;
        this.chkPCTYN.Checked = false;
        this.chkForUpdate.Checked = false;
        this.chkForOperation.Checked = false;
        this.lblError.Text = "";
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
        this.Search();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            this.chkForOperation.Checked = true;
            this.chkForUpdate.Checked = true;
            Country runMethod = new Country();
            runMethod.GetCountryInfo(this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["Country"].ToString());
            this.txtCountry.Text = this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["Country"].ToString();
            this.txtCountryEng.Text = runMethod.EnglishName;
            this.txtCountryChi.Text = runMethod.ChineseName;
            this.chkPCTYN.Checked = runMethod.PCTYN;
        }
        catch (NullReferenceException)
        {
        }
        this.setButtons(string.Empty);
    }

    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "Countries");
        if (!canView.CanView())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        else
        {
            this.setButtons(string.Empty);
            this.setTextReadOnly(false);
            if (!this.Page.IsPostBack)
            {
                ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.MaintenanceFiles + " | " + ISitemap.CountryAdd.ToString();

                this.btnDelete.Attributes.Add("onClick", "return confirm('" + Messages.DeleteConfirmation.ToString() + "')");
            }
        }
    }

    protected void Search()
    {
        CountriesFacade facade = new CountriesFacade();
        SqlParameter[] sqlparam = new SqlParameter[3];
        sqlparam[0] = new SqlParameter();
        sqlparam[0].ParameterName = "@Country";
        sqlparam[0].Value = this.txtSearchCountry.Text.ToString();
        sqlparam[1] = new SqlParameter();
        sqlparam[1].ParameterName = "@EnglishName";
        sqlparam[1].Value = this.txtSearchEnglishName.Text.ToString();
        sqlparam[2] = new SqlParameter();
        sqlparam[2].ParameterName = "@ChineseName";
        sqlparam[2].Value = this.txtSearchChineseName.Text.ToString();
        DbParameter[] param = sqlparam;
        DataView dv = facade.GetsCountries("_IPMS_CountrySearch_SP", param).DefaultView;
        this.GridView1.DataSourceID = string.Empty;
        this.GridView1.DataSource = dv;
        this.GridView1.DataBind();
    }

    public void setButtons(string action)
    {
        //switch (action)
        //{
        //    case "new":
        //        this.btnNew.Enabled = false;
        //        this.btnModify.Enabled = false;
        //        this.btnSave.Enabled = true;
        //        this.btnDelete.Enabled = false;
        //        return;

        //    case "modify":
        //        this.btnNew.Enabled = false;
        //        this.btnModify.Enabled = false;
        //        this.btnSave.Enabled = true;
        //        this.btnDelete.Enabled = false;
        //        return;
        //}
        this.btnNew.Enabled = true;
        this.btnModify.Enabled = this.txtCountry.Text.Length != 0;
        this.btnSave.Enabled = true ;
        this.btnDelete.Enabled = true;
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.txtCountry.ReadOnly = bBool;
        this.txtCountryEng.ReadOnly = bBool;
        this.txtCountryChi.ReadOnly = bBool;
        this.chkPCTYN.Enabled = !bBool;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Countries.aspx");
    }
}
